home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / miscellaneous / science / maths / calc / source / calc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-07  |  4.7 KB  |  222 lines

  1. /*
  2.  * Copyright (c) 1994 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Modified for the Amiga by Steve Leblanc, Oct 1995
  7.  *
  8.  * Arbitrary precision calculator.
  9.  */
  10.  
  11. #include <signal.h>
  12. #include <sys/types.h>
  13. #include <fcntl.h>
  14.  
  15. #include "calc.h"
  16. #include "func.h"
  17. #include "opcodes.h"
  18. #include "config.h"
  19. #include "token.h"
  20. #include "symbol.h"
  21.  
  22.  
  23. /*
  24.  * Common definitions
  25.  */
  26. long maxprint;        /* number of elements to print */
  27. int abortlevel;        /* current level of aborts */
  28. BOOL inputwait;        /* TRUE if in a terminal input wait */
  29. jmp_buf jmpbuf;        /* for errors */
  30.  
  31. static int q_flag = FALSE;    /* TRUE => don't execute rc files */
  32.  
  33. /* AMIGA: got rid of the bindings path variable */
  34. char *calcpath;        /* CALCPATH or default */
  35. char *calcrc;        /* CALCRC or default */
  36. char *helpdir;        /* CALCHELP or default */
  37. char *home;        /* HOME or default */
  38. static char *pager;    /* PAGER or default */
  39. int stdin_tty = TRUE;    /* TRUE if stdin is a tty */
  40.  
  41. static void intint();    /* interrupt routine */
  42. static void initenv();    /* initialize/default special environment vars */
  43.  
  44. #include <stdlib.h>
  45.  
  46. extern void file_init();
  47. extern void zio_init();
  48.  
  49.  
  50. /*
  51.  * Top level calculator routine.
  52.  */
  53. main(int argc,char **argv)
  54. {
  55.     static char *str;    /* current option string or expression */
  56.     char cmdbuf[MAXCMD+1];    /* command line expression */
  57.  
  58.     file_init();
  59.     zio_init();
  60.     initenv();
  61.     argc--;
  62.     argv++;
  63.     while ((argc > 0) && (**argv == '-')) {
  64.         for (str = &argv[0][1]; *str; str++) switch (*str) {
  65.             case 'h':
  66.                 givehelp(DEFAULTCALCHELP);
  67.                 exit(0);
  68.                 break;
  69.             case 'q':
  70.                 q_flag = TRUE;
  71.                 break;
  72.             default:
  73.                 printf("Unknown option\n");
  74.                 exit(1);
  75.         }
  76.         argc--;
  77.         argv++;
  78.     }
  79.     str = cmdbuf;
  80.     *str = '\0';
  81.     while (--argc >= 0) {
  82.         *str++ = ' ';
  83.         strcpy(str, *argv++);
  84.         str += strlen(str);
  85.         str[0] = '\n';
  86.         str[1] = '\0';
  87.     }
  88.     str = cmdbuf;
  89.  
  90.     if (*str == '\0') {
  91.         str = NULL;
  92.         stdin_tty = 1;    /* AMIGA: assume a tty if no cmd_arg */
  93.  
  94.         if (stdin_tty) {
  95.             version(stdout);
  96.             printf("[Type \"exit\" to exit, or \"help\" for help.]\n\n");
  97.         }
  98.     }
  99.     if (setjmp(jmpbuf) == 0) {
  100.         initmasks();
  101.         inittokens();
  102.         initglobals();
  103.         initfunctions();
  104.         initstack();
  105.         resetinput();
  106.         math_cleardiversions();
  107.         math_setfp(stdout);
  108.         math_setmode(MODE_INITIAL);
  109.         math_setdigits((long)DISPLAY_DEFAULT);
  110.         maxprint = MAXPRINT_DEFAULT;
  111.         _epsilon_ = atoq(EPSILON_DEFAULT);
  112.         _epsilonprec_ = qprecision(_epsilon_);
  113.         if (str || !stdin_tty) {
  114.             if (q_flag == FALSE) {
  115.                 runrcfiles();
  116.                 q_flag = TRUE;
  117.             }
  118.             if (str)
  119.                 (void) openstring(str);
  120.             else
  121.                 (void) openterminal();
  122.             getcommands(FALSE);
  123.             exit(0);
  124.         }
  125.     }
  126.     if (str)
  127.         exit(1);
  128.     abortlevel = 0;
  129.     _math_abort_ = FALSE;
  130.     inputwait = FALSE;
  131.     (void) signal(SIGINT, intint);
  132.     math_cleardiversions();
  133.     math_setfp(stdout);
  134.     resetscopes();
  135.     resetinput();
  136.     if (q_flag == FALSE) {
  137.         runrcfiles();
  138.         q_flag = TRUE;
  139.     }
  140.     (void) openterminal();
  141.     getcommands(TRUE);
  142.     exit(0);
  143.     /*NOTREACHED*/
  144. }
  145.  
  146.  
  147. /*
  148.  * initenv - obtain CALCPATH, CALCRC, CALCHELP, HOME, and PAGER values
  149.  *
  150.  * If CALCPATH, CALCRC, CALCHELP, or PAGER do not exist,
  151.  * use the default values.  If PAGER is an empty string, also
  152.  * use a default value. If HOME does not exist, or is empty, use S:
  153.  *
  154.  * AMIGA: Heavily modified to use the environment variables I decided
  155.  *        for the Amiga.
  156.  */
  157. static void
  158. initenv()
  159. {
  160.     /* determine the CALCPATH value */
  161.     calcpath = getenv("CALCPATH");
  162.     if (calcpath == NULL)
  163.         calcpath = DEFAULTCALCPATH;
  164.  
  165.     /* determine the CALCRC value */
  166.     calcrc = getenv("CALCRC");
  167.     if (calcrc == NULL)
  168.         calcrc = DEFAULTCALCRC;
  169.  
  170.     /* determine the CALCHELP value */
  171.     helpdir = getenv("CALCHELP");
  172.     if(helpdir == NULL)
  173.         helpdir = DEFAULTHELPDIR;
  174.  
  175.     /* determine the HOME value */
  176.     home = getenv("HOME");
  177.     if (home == NULL || home[0] == '\0')
  178.         home = DEFAULTHOME;
  179.   
  180.     /* determine the PAGER value */
  181.     pager = getenv("PAGER");
  182.     if (pager == NULL || *pager == '\0')
  183.         pager = DEFAULTCALCPAGER;
  184. }
  185.  
  186. void
  187. givehelp(char *type)
  188. {
  189.     char *helpcmd;        /* what to execute to print help */
  190.  
  191.     /* catch the case where we just print the index */
  192.     if (type == NULL) {
  193.         type = DEFAULTCALCHELP;        /* the help index file */
  194.     }
  195.  
  196.     /* AMIGA: Command string modified to work on Amiga */
  197.   /* form the help command name */
  198.     helpcmd = (char *)malloc(strlen(pager)+1+strlen(helpdir)+1+strlen(type)+1);
  199.     sprintf(helpcmd,"%s %s%s",pager,helpdir,type);
  200.  
  201.     /* execute the help command */
  202.     system(helpcmd);
  203.     free(helpcmd);
  204. }
  205.  
  206. /*
  207.  * Interrupt routine.
  208.  */
  209. /*ARGSUSED*/
  210. static void
  211. intint(int arg)
  212. {
  213.     (void)signal(SIGINT,intint);
  214.     if (inputwait || (++abortlevel >= ABORT_NOW))
  215.         math_error("\nABORT");
  216.     if (abortlevel >= ABORT_MATH)
  217.         _math_abort_ = TRUE;
  218.     printf("\n[Abort level %d]\n", abortlevel);
  219. }
  220.  
  221. /* END CODE */
  222.